﻿namespace FFF.Test.Application.AppServices.FW
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Text;
    using System.Linq;
    using System.Threading.Tasks;
    using Abp.Application.Services;
    using Abp.Application.Services.Dto;
    using Abp.Authorization;
    using Abp.UI;
    using Abp.Web.Models;
    using Abp.Domain.Uow;
    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Mvc;
    using Abp.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore;
    using AutoMapper.QueryableExtensions;
    using FFF.Test.Application.AppServices;
    using FFF.Test.Application.DomainServices;
    using FFF.Test.DataAccess.Framework;
    using FFF.Test.Application.AppServices.PublicService.Sessions;
    using FFF.Test.Application.Framework.Dto;
    using Ydm.Helper;
    using Ydm.Extensions;
    using FFF.Test.Application.PublicService.AppService.TreeGrid;
    using FFF.Test.Application.AppServices.PublicService.Grid;
    using FFF.Test.Application.DomainServices.FW;
    using FFF.Test.Application.Repositories.FW;
    using FFF.Test.Entity.FW;
    using FFF.Test.Entity.FW.Dto;
    using FFF.Test.Entity.FW.View;

    /// <summary>
    /// 标准角色用户中间表应用层服务
    /// </summary>
    [YdmApiVersion(Version = "FW")]
    public class FWStandardRole2USerAppService : YdmAbpCurdAppServiceBase<FWStandardRole2USerEntity, Guid, FWStandardRole2USerCreateInputDto, FWStandardRole2USerUpdateInputDto, FWStandardRole2USerDto, ViewFWStandardRole2USer, ViewFWStandardRole2USer>, IFWStandardRole2USerAppService
    {
        private readonly IFWStandardRole2USerDomainService _fWStandardRole2USerDomainService;

        private readonly IFWStandardRole2USerRepository _fWStandardRole2USerRepository;

        private readonly ISessionAppService _sessionAppservice;

        /// <summary>
        /// Initializes a new instance of the <see cref="FWStandardRole2USerAppService"/> class.
        /// 构造方法
        /// </summary>
        /// <param name="fWStandardRole2USerRepository">标准角色用户中间表仓储</param>
        /// <param name="fWStandardRole2USerDomainService">标准角色用户中间表领域层</param>
        /// <param name="sessionAppservice">session</param>
        public FWStandardRole2USerAppService(
            IFWStandardRole2USerRepository fWStandardRole2USerRepository,
            IFWStandardRole2USerDomainService fWStandardRole2USerDomainService,
            ISessionAppService sessionAppservice
        )
        {
            this._fWStandardRole2USerRepository = fWStandardRole2USerRepository;
            this._fWStandardRole2USerDomainService = fWStandardRole2USerDomainService;
            this._sessionAppservice = sessionAppservice;
        }

        #region 框架方法

        /// <summary>
        /// 导入[标准角色用户中间表]表格数据
        /// </summary>
        /// <param name="inDto">获取导出表格数据输入dto</param>
        /// <returns>dto对象</returns>
        [HttpPost]
        [UnitOfWork(isTransactional: true)]
        [YdmApiAuthor(FunctionCode = "FW_StandardRole2USer", ActionCode = "01")]
        public override async Task<ImportOutDto> ImportExcel(ImportInDto inDto)
        {
            var outDto = await this._fWStandardRole2USerRepository.ImportExcelAsync(inDto);
            return outDto;
        }

        /// <summary>
        /// 导出[标准角色用户中间表]表格数据
        /// </summary>
        /// <param name="inDto">获取导出表格数据输入dto</param>
        /// <returns>dto对象</returns>
        [HttpGet]
        [WrapResult(WrapOnSuccess = false, WrapOnError = false)]
        [UnitOfWork(isTransactional: false)]
        [YdmApiAuthor(FunctionCode = "FW_StandardRole2USer", ActionCode = "92")]
        public override async Task<FileContentResult> GetGridExcel(GetGridListInDto inDto)
        {
            var fileResult = await this._fWStandardRole2USerRepository.GetGridExcelAsync(inDto);
            return fileResult;
        }

        /// <summary>
        /// 获取  [标准角色用户中间表] 表格数据
        /// </summary>
        /// <param name="inDto">表格数据输入的Dto</param>
        /// <returns>分页信息</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: false)]
        [YdmApiAuthor(FunctionCode = "FW_StandardRole2USer", ActionCode = "00")]
        public override async Task<GridResult> GetGridDto(GetGridListInDto inDto)
        {
            var gridResult = await this._fWStandardRole2USerRepository.GetGridDtoAsync(inDto);
            return gridResult;
        }

        /// <summary>
        /// 获取单个  [标准角色用户中间表] 视图的dto
        /// </summary>
        /// <param name="idDto">idDto集合</param>
        /// <returns>单个对象的dto</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: false)]
        [YdmApiAuthor(FunctionCode = "FW_StandardRole2USer", ActionCode = "00")]
        public override async Task<ViewFWStandardRole2USer> GetViewDto(PrimaryDto idDto)
        {
            var id = (Guid)idDto.Id.CastType(typeof(Guid));
            var dto = await this._fWStandardRole2USerRepository.GetViewDtoAsync(id);
            return dto;
        }

        /// <summary>
        /// 获取  [标准角色用户中间表] 视图分页的信息
        /// </summary>
        /// <param name="pagedInputDto">分页输入的Dto</param>
        /// <returns>分页信息</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: false)]
        [YdmApiAuthor(FunctionCode = "FW_StandardRole2USer", ActionCode = "00")]
        public override async Task<MyPagedResult<ViewFWStandardRole2USer>> GetViewPage(PagedInputDto pagedInputDto)
        {
            var pagedResult = await this._fWStandardRole2USerRepository.GetViewPageAsync(pagedInputDto);
            return pagedResult;
        }

        /// <summary>
        /// 创建  [标准角色用户中间表]
        /// </summary>
        /// <param name="input">创建输入实体</param>
        /// <returns>输出dto</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: true)]
        [YdmApiAuthor(FunctionCode = "FW_StandardRole2USer", ActionCode = "01")]
        public override async Task<FWStandardRole2USerDto> CreateByDto(FWStandardRole2USerCreateInputDto input)
        {
            var dto = await this._fWStandardRole2USerRepository.CreateByDtoAsync(input);
            return dto;
        }

        /// <summary>
        /// 更新  [标准角色用户中间表]
        /// </summary>
        /// <param name="input">更新的实体的对象</param>
        /// <returns>更新后的对象</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: true)]
        [YdmApiAuthor(FunctionCode = "FW_StandardRole2USer", ActionCode = "02")]
        public override async Task<FWStandardRole2USerDto> UpdateByDto(FWStandardRole2USerUpdateInputDto input)
        {
            var dto = await this._fWStandardRole2USerRepository.UpdateByDtoAsync(input);
            return dto;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="idDto">idDto集合</param>
        /// <returns>空</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: true)]
        [YdmApiAuthor(FunctionCode = "FW_StandardRole2USer", ActionCode = "03")]
        public override async Task DeleteByDto(PrimaryDto idDto)
        {
            var id = (Guid)idDto.Id.CastType(typeof(Guid));
            await this._fWStandardRole2USerRepository.DeleteByIdAsync(id);
        }

        /// <summary>
        /// 批量删除  [标准角色用户中间表] 传递包含id的dto
        /// </summary>
        /// <param name="idDto">idDto集合</param>
        /// <returns> task 空值</returns>
        [HttpPost]
        [Authorize(Roles = "admin")]
        [UnitOfWork(isTransactional: true)]
        [YdmApiAuthor(FunctionCode = "FW_StandardRole2USer", ActionCode = "03")]
        public override async Task BatchDelete(PrimaryDto idDto)
        {
            string strId = idDto.Id;
            IEnumerable<string> ids = strId.Split(",").Select(x => x.Replace("'", string.Empty));
            List<Guid> newIds = new List<Guid>();
            foreach (var id in ids)
            {
                await this._fWStandardRole2USerRepository.DeleteAsync((Guid)id.CastType(typeof(Guid)));
            }
        }
        #endregion
    }
}
